<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/7
 * Time: 14:38
 * 原始视频混音
 */
require_once dirname(__DIR__)."/common.php";
use app\cls\tom\Lock;
use app\cls\tom\FFmpeg;
use app\lib\db\DbException;
use app\cls\tom\SysResource;
use app\cls\tom\Process;
use app\cls\tom\Tools;
use app\model\Video;

$process = new Process(__FILE__);
$process->setProcessNum(5);
$process->initProcess();
$process->isTryError = false;
$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);
$ffmpeg = new FFmpeg(true);
/*===============
 *初始化模型
 ================*/
$videoModel = new Video($db);
$cosUploadConfig = \app\cls\tom\Config::load("cos_upload");
$lock = new Lock("pgc_material_audio_video_transcode");
while(true){
    try{
        $readyList = $videoModel->video_transcode(10);
        $date1=date("Y",time());
        $date2=date("m",time());
        $date3=date("d",time());
        $time_dir=$date1."/".$date2."/".$date3;
//        var_dump($readyList);exit;
        foreach($readyList as &$row){
//            var_dump($lock->unLock($row["id"]));continue;
            $lockStatus = $lock->tryLock($row["id"]);
//            var_dump($lockStatus);exit;
            if($lockStatus){
                $oriVideoUrl = $row["video_url"];
                $quality_video_url = $row["quality_video_url"];
                $user_material_id = $row["user_material_id"];
                $totalStatus = [];
                //1.有音频视频转码
                if(empty($row["quality_video_url"])){
                    $outPutPath2 = CDN_PATH."/transCodeV5/".$time_dir."/".pathinfo($oriVideoUrl,PATHINFO_BASENAME);
                    $outPutPath1 = CDN_TMP1.$time_dir."/".pathinfo($oriVideoUrl,PATHINFO_BASENAME);
                    $oriVideoPath = str_replace(CDN_HOST,CDN_PATH,$oriVideoUrl);
                    $re = $ffmpeg->transCode($oriVideoPath,$outPutPath1,$outPutPath2,2);
//                    $re = $ffmpeg->transCode($oriVideoPath,$outPutPath1);
//                    var_dump($re);exit;
                    if(!$re){
                        Tools::writeLog(["msg"=>"有音频视频转码失败","detail"=>["media_id"=>$row["id"],"msg"=>$ffmpeg->getLastError()],"ori_mix"]);
                        $db->update("quality_video",["transcode_status"=>1,"transcode_msg"=>$ffmpeg->getLastError(),"transcode_count"=>"!transcode_count+1"],["id"=>$row["id"]]);
                        $lock->unLock($row["id"]);
                        continue;
                    }
                    $gaibian_video=str_replace(CDN_PATH,CDN_HOST,$outPutPath2);
                    $db->reconnect();
                    $res1=$db->update("quality_video",["quality_video_url"=>$gaibian_video,],["id"=>$row["id"]]);
                    if(!$res1){
                        $db->update("quality_video",["transcode_msg"=>"插入数据库失败"],["id"=>$row["id"]]);
                    }
                    //再去改用户上传表的地址
                    $db->update("user_material",["video_url"=>$gaibian_video],["id"=>$row["user_material_id"]]);
                    $totalStatus[] = 1;
                }else{
                    $totalStatus[] = 1;
                }
                if(count($totalStatus) == 1){
                    $db->update("quality_video",["transcode_status"=>2,"transcode_msg"=>"转码成功","transcode_count"=>"!transcode_count+1"],["id"=>$row["id"]]);
                }
                $lock->unLock($row["id"]);
            }
        }
    }catch (DBException $e){
        echo $e->getMessage();
//        $process->restartProcess();
    }catch (RedisException $e){

    }catch (Exception $e){
        if(!empty($row)){
            $db->update("quality_video",["transcode_status"=>1,"transcode_msg"=>$e->getMessage(),"transcode_count"=>"!transcode_count+1"],["id"=>$row["id"]]);
            $lock->unLock($row["id"]);
        }
        Tools::writeLog(["msg"=>"pgc转码失败","detail"=>["msg"=>$e->getMessage(),"id"=>$id],"mix"]);
    }
    sleep(2);
    $process->checkProcessTime(true);
    $db->reconnect();
}
